<?php
namespace ac\modules\control\controllers;

use Yii;
use ac\controllers\AuthController;
use app\models\Admin;
use ac\component\exception\ErrMap;
use ac\component\tool\Tool;
use app\models\OperationLog;


class AdminController extends AuthController
{

	/*管理员列表*/
    public function actionAdminList()
    {
        $adminList = Admin::find()->select(['admin_id', 'username', '{{%admin}}.role_id', 'lastloginip', 'lastlogin_time', 'creater', 'state', 'logintimes', 'add_time'])
                        ->joinWith('adminRole')
                        ->orderBy(['issuper' => SORT_ASC, '{{%admin}}.sort' => SORT_DESC])
                        ->asArray()->all();
        foreach($adminList as $k => $v){
            if($v['lastloginip'] != 0){
                // $adminList[$k]['lastloginip'] = long2ip($v['lastloginip']);
                $adminList[$k]['lastloginip'] = $v['lastloginip'];
            }
        }
            return Tool::showRes([0, '获取成功'], $adminList);

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*添加管理员*/
    public function actionAddAdmin()
    {
        if(Yii::$app->request->isPost){
            // P($this->post);
            $Admin = new Admin;
            if(isset($this->post['Admin']['allowlogindate'])){
                $this->post['Admin']['allowlogindate'] = implode(',', $this->post['Admin']['allowlogindate']);
            }
            if($Admin->addAdmin($this->post)){
                $backInfo = [0, '添加管理员成功'];//返回信息
            }else{
                if($Admin->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $Admin->getErrors();//异常信息
                    if(array_key_exists("repeatUsername", $exception)){
                        $backInfo = ErrMap::getError(10011);//返回信息
                    };
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*
    修改管理员
    */
    public function actionModifyAdmin()
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            $get = Yii::$app->request->get();
            $id = isset($get['id'])?$get['id']:'';
            if($id){
                $admin = Admin::find()
                         ->select(['admin_id', 'username', 'role_id', 'allowlogindate', 'state'])
                         ->where('admin_id = :id', [':id' => $id])->asArray()->one();
                if(!is_null($admin)){
                    return Tool::showRes([0, '获取成功'], $admin);
                }else{
                    return Tool::showRes(ErrMap::getError(10010));
                };
            }
        }

        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['Admin']['id'])?intval($this->post['Admin']['id']):'';
            if($id === 1){
                $backInfo = ErrMap::getError(10012);//返回信息
            }else if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $Admin = new Admin;
                if(isset($this->post['Admin']['allowlogindate'])){
                    $this->post['Admin']['allowlogindate'] = implode(',', $this->post['Admin']['allowlogindate']);
                }
                // P($this->post);
                if($Admin->modifyAdmin($this->post, $id)){
                    $backInfo = [0, '修改管理员成功'];//返回信息
                }else{
                    if($Admin->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $Admin->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*排序*/
    public function actionSetSort()
    {
        if(Yii::$app->request->isPost){
            $sort = Yii::$app->request->post('sort');
            // P($sort);
            if(!empty($sort) and is_array($sort)){
                $Admin = new Admin;
                if($Admin->setSort($sort)){
                    $backInfo = [0, '设置排序成功'];//返回信息
                }else{
                    if($Admin->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $Admin->getErrors();//异常信息
                    }
                }
            }
            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
                return $res;
            };
        }
        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除,（真删除）*/
    public function actionDeleteAdmin()
    {
        $id = isset($this->post['id'])?$this->post['id']:'';
        if($id){
            $admin = Admin::find()->where('admin_id = :id', [':id' => $id])->one();
            if(!is_null($admin)){
                if($admin->issuper == 1){
                    $backInfo = ErrMap::getError(10013, '超级管理员不能删除');//返回信息
                }else{
                    if($admin->delete()){
                        $backInfo = [0, '删除管理员成功'];//返回信息
                    }else{
                        $backInfo = ErrMap::getError(10013, '删除管理员失败');//返回信息
                    }
                }
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }


}